/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newItem.java
 *
 * Created on May 8, 2013, 10:42:35 PM
 */
package democarmanagement;

import com.mysql.jdbc.Connection;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Statement;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Minhdaica
 */
public class newItem extends javax.swing.JFrame {

    private String ma_hang;
    private String chi_tiet;

    /** Creates new form newItem */
    public newItem() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jCodetxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jImgBtn = new javax.swing.JButton();
        jAddbtn = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jDescriptionTA = new javax.swing.JTextArea();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Mã Hàng:");

        jCodetxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCodetxtActionPerformed(evt);
            }
        });

        jLabel2.setText("Mô tả chi tiết:");

        jLabel3.setText("Thông tin hàng mới");

        jLabel4.setText("Hình ảnh:");

        jImgBtn.setText("Click");
        jImgBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jImgBtnActionPerformed(evt);
            }
        });

        jAddbtn.setText("Thêm");
        jAddbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAddbtnActionPerformed(evt);
            }
        });

        jDescriptionTA.setColumns(20);
        jDescriptionTA.setRows(5);
        jScrollPane2.setViewportView(jDescriptionTA);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jImgBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCodetxt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(208, 208, 208))
            .addGroup(layout.createSequentialGroup()
                .addGap(263, 263, 263)
                .addComponent(jAddbtn)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jCodetxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jImgBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jAddbtn)
                .addGap(105, 105, 105))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jAddbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAddbtnActionPerformed
        Car newCar = new Car(jCodetxt.getText());
        newCar.setChiTiet(jDescriptionTA.getText());
        newCar.AddNewCar();
        this.setVisible(false);
        new NotifyOK().setVisible(true);


    }//GEN-LAST:event_jAddbtnActionPerformed

    private void jCodetxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCodetxtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCodetxtActionPerformed

    private void jImgBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jImgBtnActionPerformed
        readFile();
    }//GEN-LAST:event_jImgBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new newItem().setVisible(true);
            }
        });
    }

    public void setJCodetxt(String jCodetxt) {
        this.jCodetxt.setText(jCodetxt);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAddbtn;
    private javax.swing.JTextField jCodetxt;
    private javax.swing.JTextArea jDescriptionTA;
    private javax.swing.JButton jImgBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration//GEN-END:variables
    void readFile() {
        JFileChooser j = new JFileChooser();
        int open = j.showOpenDialog(null);
        String fileName = j.getSelectedFile().getPath();
        System.out.println(fileName);
        File f = j.getSelectedFile();
        try {
            // the line that reads the image file
            BufferedImage image;


            // work with the image here ...
            image = ImageIO.read(new File(fileName));
            File files[] = j.getSelectedFiles();
            JPanel panel = new JPanel(new GridLayout(files.length, 1));
            JLabel lable = new JLabel(new ImageIcon(image));
            panel.add(lable);
            jScrollPane1.setViewportView(panel);

        //   jLabel1.setIcon(new ImageIcon(image));

        } catch (IOException e) {
            // log the exception
            // re-throw if desired
        }
    }
}
